package com.ml.springboot.controller;

import com.ml.springboot.pojo.Employee;
import com.ml.springboot.pojo.query.EmployeeQuery;
import com.ml.springboot.service.IEmployeeService;
import com.ml.springboot.util.PageResult;
import com.ml.springboot.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
/*    @Autowired
    private employeeMapper employeeMapper;*/
    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("/list")
    @ResponseBody
    public PageResult list(EmployeeQuery employeeQuery) {
        System.out.println("employeeController.list");
        PageResult pageResult = employeeService.list(employeeQuery);
        return pageResult;
    }
    @RequestMapping("/selectAlla")
    @ResponseBody
    public Result selectAlla(){
        List<Employee> list =employeeService.selectsuoyou();
        return Result.ok(list);
    }
    @RequestMapping("/selectAll")
    @ResponseBody
    public Result selectAll(HttpSession session) {
        System.out.println("employeeController.list");
        Employee employee1 = (Employee) session.getAttribute("employee");




       List<Employee> list=new ArrayList<>();
        list.add(employee1);
        return Result.ok(list);
    }
    @RequestMapping("/toindex")
    public String toemployeeList(HttpSession session) {
        Employee employee = (Employee) session.getAttribute("employee");
        if(employee.getRole()==1)return "index_super";
        else return "index_normal";
    }
    @RequestMapping("/toFirst")
    public String tofirst() {
       return "first";
    }
    @RequestMapping("/deleteById")
    @ResponseBody
    public Result deletedById(Integer id) {
        employeeService.deletedById(id);
        return Result.ok("删除成功");
    }

    @RequestMapping("/deleteAll")
    @ResponseBody
    public Result deletedAll(Integer[] ids) {
        employeeService.deleteAll(ids);
        return Result.ok("删除成功");
    }

    @RequestMapping("/toEmployeeAdd")
    public String toemployeeAdd() {
        return "employee_add";
    }

    @RequestMapping("/add")
    @ResponseBody
    public Result add(Employee employee) {
        employeeService.add(employee);
        return Result.ok("添加成功");
    }
    @RequestMapping("/toemployeeList")
    public String toemployeeList(Employee employee){
        return "employee_list";
    }
    @RequestMapping("/toEmployeeUpdate")
    public String toemployeeUpdate(Employee employee) {
        return "employee_update";
    }

    @RequestMapping("/selectById")
    @ResponseBody
    public Result selectById(Integer id) {
        Employee employee = employeeService.selectById(id);
        return Result.ok(employee);
    }

    @RequestMapping("/update")
    @ResponseBody
    public Result update(Employee employee) {
        employeeService.update(employee);
        return Result.ok("修改成功");
    }

    @RequestMapping("/toLogin")
    public String toLogin() {
        return "login";
    }

    @RequestMapping("/login")
    @ResponseBody
    public Result login(String name, String password, String code, HttpSession session) {

        Employee employee = employeeService.login(name, password);
        if(employee == null) {
            return Result.error("用户名或密码错误");
        }
        //状态检测
        if(employee.getStatus() == 0){
            return Result.error("您无权登录");
        }

        session.setAttribute("employee", employee);


        return Result.ok("登录成功");
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        //移除凭证
        session.removeAttribute("employee");
        return "redirect:/employee/toLogin";
    }
    @RequestMapping("/updateStatus")
    @ResponseBody
    public Result updateStatus(Integer id, Integer status) {
        employeeService.updateStatus(id,status);
        return Result.ok("更新成功");
    }
}
